/**
 * @author Sebastian
 * @version 2.0
 * @date 2024/7/22 20:45
 */
// 长度最小的子数组
    // https://leetcode.cn/problems/minimum-size-subarray-sum/submissions/484014494/
    // 前缀和+滑动窗口
public class Solution209 {
    public int minSubArrayLen(int target, int[] nums) {
        int res = Integer.MAX_VALUE;
        int sum=0;
        int left = 0;
        for (int right = 0; right < nums.length; right++) {
            sum += nums[right];
            while (sum >= target) {
                res = Math.min(res, right - left + 1);
                sum -= nums[left++];
            }
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }
}
